package com.auerdi.ui.fragment.c_officer.child;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.auerdi.R;
import com.example.egghttp.EggLog.L;
import com.example.egghttp.RetrofitClient;
import com.example.egghttp.retrofit.AdService;
import com.example.egghttp.retrofit.beans.BOb;
import com.example.egghttp.retrofit.beans.banner.ExperienceLists;
import com.example.egghttp.retrofit.beans.goods.OffercerProduct;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by YoKeyword on 16/6/30.
 */
public class ADisPostFragment extends SupportFragment {

    Unbinder unbinder;
    @BindView(R.id.recy)
    RecyclerView recy;


    private List<OffercerProduct> mOffercerList = new ArrayList<>();

    public static ADisPostFragment newInstance() {

        Bundle args = new Bundle();
        ADisPostFragment fragment = new ADisPostFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.officer_dis, container, false);
        unbinder = ButterKnife.bind(this, view);
        recy.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));


        recy.setHasFixedSize(true);

        recy.setAdapter(new CommonAdapter<OffercerProduct>(getContext(), R.layout.item_officer_dis, mOffercerList) {
            @Override
            protected void convert(ViewHolder viewHolder, final OffercerProduct item, int position) {


                ImageView img = viewHolder.getView(R.id.img);
                ImageView gpic = viewHolder.getView(R.id.gpic);

                viewHolder.setText(R.id.title, item.getTitle());
                viewHolder.setText(R.id.stitle, item.getNick());
                viewHolder.setText(R.id.like, item.getDianzan_count());
                Picasso.get().load(item.getPic_zhutu()).error(R.mipmap.logo).placeholder(R.mipmap.logo).into(img);
                Picasso.get().load(item.getHead()).error(R.mipmap.logo).placeholder(R.mipmap.logo).into(gpic);


            }
        });

//        recy.setAdapter(new CommonAdapter<Product>(getContext(), R.layout.item_officer_dis, items) {
//            @Override
//            protected void convert(ViewHolder viewHolder, final Product item, int position) {
//
//
//                TextView title = viewHolder.getView(R.id.title);
//                title.setText("position: " +position);
//                title.setMinimumHeight(100 + (position % 3) * 30);
//
//
//
//            }
//        });


        RetrofitClient.getPDAClient().create(AdService.class).experienceLists()               //获取Observable对象
                .subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BOb<ExperienceLists>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                        L.d("hcia", "e:" + e);

                    }

                    @Override
                    public void onNext(BOb<ExperienceLists> experienceListsBOb) {
                        L.d("hcia", "experienceListsBOb.getBody().getLists().size():" + experienceListsBOb.getBody().getLists().size());

                        List<OffercerProduct> offercerList = experienceListsBOb.getBody().getLists();


                        mOffercerList.clear();
                        mOffercerList.addAll(offercerList);
                        recy.getAdapter().notifyDataSetChanged();
                    }
                });


        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }
}
